package com.artron.mediaartron.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.IntDef;
import android.view.Menu;

import com.artron.baselib.base.BaseFragment;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.entity.AddressData;
import com.artron.mediaartron.data.entity.Invoice;
import com.artron.mediaartron.data.entity.LoginData;
import com.artron.mediaartron.data.entity.ShoppingCartItemData;
import com.artron.mediaartron.data.entity.WorksData.WorksListBean;
import com.artron.mediaartron.ui.callback.MainActivityCallback;
import com.artron.mediaartron.ui.fragment.center.AccountManageFragment;
import com.artron.mediaartron.ui.fragment.center.AlterationPhoneFragment;
import com.artron.mediaartron.ui.fragment.center.BindPhoneFragment;
import com.artron.mediaartron.ui.fragment.center.ChangePasswordFragment;
import com.artron.mediaartron.ui.fragment.center.CouponFragment;
import com.artron.mediaartron.ui.fragment.center.FeedbackFragment;
import com.artron.mediaartron.ui.fragment.center.ForgotPasswordFragment;
import com.artron.mediaartron.ui.fragment.center.HelpListFragment;
import com.artron.mediaartron.ui.fragment.center.MaterialManagerFragment;
import com.artron.mediaartron.ui.fragment.center.MyOrdersFragment;
import com.artron.mediaartron.ui.fragment.center.SettingFragment;
import com.artron.mediaartron.ui.fragment.center.UserInfoFragment;
import com.artron.mediaartron.ui.fragment.center.WorksFinishDetailFragment;
import com.artron.mediaartron.ui.fragment.center.WorksFragment;
import com.artron.mediaartron.ui.fragment.dialog.NormalDialogFragment;
import com.artron.mediaartron.ui.fragment.general.HelpFragment;
import com.artron.mediaartron.ui.fragment.general.TextEditFragment;
import com.artron.mediaartron.ui.fragment.made.AddVideoFragment;
import com.artron.mediaartron.ui.fragment.order.InvoiceFragment;
import com.artron.mediaartron.ui.fragment.order.OrderAddAddressFragment;
import com.artron.mediaartron.ui.fragment.order.OrderAddressFragment;
import com.artron.mediaartron.ui.fragment.order.OrderDetailFragment;
import com.artron.mediaartron.ui.fragment.order.OrderFragment;
import com.artron.mediaartron.ui.fragment.order.OrderFromShoppingCartFragment;
import com.artron.mediaartron.ui.fragment.order.OrderSuccessFragment;
import com.artron.mediaartron.ui.fragment.shoppingcart.ShoppingCartFragment;
import com.bilibili.boxing.Boxing;
import com.bilibili.boxing.model.entity.BaseMedia;
import com.bilibili.boxing.model.entity.impl.ImageMedia;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import rx.functions.Action0;

import static com.artron.mediaartron.ui.fragment.center.AlterationPhoneFragment.CHANGE_PHONE;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-13.
 */

public class GeneralActivity extends AppBaseActivity {

    public static final String TYPE = "Type";
    public static final String TITLE = "Title";
    public static final String CONTENT = "Content";
    private static final String DATA = "Data";

    public static final int HELP = 6000;
    public static final int TEXT_EDIT = 6001;
    public static final int ORDER = 6002;
    public static final int ORDER_ADDRESS = 6003;
    public static final int ORDER_ADD_ADDRESS = 6004;
    public static final int USER_INFO = 6005;
    public static final int INVOICE = 6006;
    public static final int ORDER_DETAIL = 6007;
    public static final int ORDER_SUCCESS = 6008;
    public static final int SHOPPING_CART = 6009;
    public static final int MY_ORDERS = 6010;
    public static final int ORDER_FROM_SHOPPING_CART = 6011;
    public static final int FEEDBACK = 6012;
    public static final int ADD_VIDEO = 6013;
    public static final int SETTING = 6014;
    public static final int HELP_LIST = 6015;
    public static final int ACCOUNT_MANAGE = 6016;
    public static final int ACCOUNT_CHANGE_PASSWORD = 6017;
    public static final int MY_WORKS = 6018;
    public static final int MY_WORKS_DETAIL = 6019;
    public static final int MATERIAL_REPERTORY = 6020;
    public static final int BIND_PHONE = 6021;
    public static final int ALTERATION_PHONE = 6022;
    public static final int FORGOT_PASSWORD = 6065;
    public static final int COUPON = 6066;


    @IntDef({HELP, TEXT_EDIT, ORDER, ORDER_ADDRESS, ORDER_ADD_ADDRESS, USER_INFO,
            INVOICE, ORDER_DETAIL, ORDER_SUCCESS, SHOPPING_CART, MY_ORDERS,
            ORDER_FROM_SHOPPING_CART, FEEDBACK, ADD_VIDEO, SETTING, HELP_LIST,
            ACCOUNT_MANAGE, ACCOUNT_CHANGE_PASSWORD, MY_WORKS, MY_WORKS_DETAIL,
            MATERIAL_REPERTORY, BIND_PHONE, ALTERATION_PHONE, FORGOT_PASSWORD,COUPON})
    @Retention(RetentionPolicy.SOURCE)
    @interface FragmentType {
    }

    public static final int REQUEST_CODE = 10000;


    public static void start(Context context, @FragmentType int type, String title, String content) {
        Intent starter = new Intent(context, GeneralActivity.class);
        starter.putExtra(TYPE, type);
        starter.putExtra(TITLE, title);
        starter.putExtra(CONTENT, content);
        context.startActivity(starter);
    }

    public static void start(Context context, @FragmentType int type, String title, int position) {
        Intent starter = new Intent(context, GeneralActivity.class);
        starter.putExtra(TYPE, type);
        starter.putExtra(TITLE, title);
        starter.putExtra(CONTENT, position);
        context.startActivity(starter);
    }

    public static void start(Context context, @FragmentType int type, String title) {
        Intent starter = new Intent(context, GeneralActivity.class);
        starter.putExtra(TYPE, type);
        starter.putExtra(TITLE, title);
        context.startActivity(starter);
    }

    public static <T extends Parcelable> void start(Context context, @FragmentType int type, String title, List<T> data) {
        Intent starter = new Intent(context, GeneralActivity.class);
        starter.putExtra(TYPE, type);
        starter.putExtra(TITLE, title);
        starter.putParcelableArrayListExtra(DATA, (ArrayList<? extends Parcelable>) data);
        context.startActivity(starter);
    }

    public static <T extends Parcelable> void start(Context context, @FragmentType int type, String title, T data) {
        Intent starter = new Intent(context, GeneralActivity.class);
        starter.putExtra(TYPE, type);
        starter.putExtra(TITLE, title);
        starter.putExtra(DATA, data);
        ((Activity) context).startActivityForResult(starter, REQUEST_CODE);
    }

    public static void startForResult(Context context, @FragmentType int type, String title) {
        Intent starter = new Intent(context, GeneralActivity.class);
        starter.putExtra(TYPE, type);
        starter.putExtra(TITLE, title);
        ((Activity) context).startActivityForResult(starter, REQUEST_CODE);
    }

    public static <T extends Parcelable> void startForResult(Context context, @FragmentType int type, String title, T data) {
        Intent starter = new Intent(context, GeneralActivity.class);
        starter.putExtra(TYPE, type);
        starter.putExtra(TITLE, title);
        starter.putExtra(DATA, data);
        ((Activity) context).startActivityForResult(starter, REQUEST_CODE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_general;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        String title = getIntent().getStringExtra(TITLE);
        mTvTitle.setText(title);

        @FragmentType int type = getIntent().getIntExtra(TYPE, HELP);
        BaseFragment fragment = null;
        switch (type) {
            case HELP:
                String content1 = getIntent().getStringExtra(CONTENT);
                fragment = HelpFragment.newInstance(content1);
                break;
            case TEXT_EDIT:
                String content2 = getIntent().getStringExtra(CONTENT);
                fragment = TextEditFragment.newInstance(content2);
                break;
            case ORDER:
                fragment = OrderFragment.newInstance();
                break;
            case ORDER_ADDRESS:
                fragment = OrderAddressFragment.newInstance();
                break;
            case ORDER_ADD_ADDRESS:
                AddressData data = getIntent().getParcelableExtra(DATA);
                fragment = OrderAddAddressFragment.newInstance(data);
                break;
            case INVOICE:
                Invoice invoice = getIntent().getParcelableExtra(DATA);
                fragment = InvoiceFragment.newInstance(invoice);
                break;
            case ORDER_DETAIL:
                String detailId = getIntent().getStringExtra(CONTENT);
                fragment = OrderDetailFragment.newInstance(detailId);
                break;
            case ORDER_SUCCESS:
                String id = getIntent().getStringExtra(CONTENT);
                fragment = OrderSuccessFragment.newInstance(id);
                break;
            case SHOPPING_CART:
                fragment = ShoppingCartFragment.newInstance();
                break;
            case USER_INFO://个人资料
                LoginData loginData = getIntent().getParcelableExtra(DATA);
                fragment = UserInfoFragment.newInstance(loginData);
                break;
            case MY_ORDERS:
                int position = getIntent().getIntExtra(CONTENT, 0);
                fragment = MyOrdersFragment.newInstance(position);
                break;
            case ORDER_FROM_SHOPPING_CART:
                List<ShoppingCartItemData> list = getIntent().getParcelableArrayListExtra(DATA);
                fragment = OrderFromShoppingCartFragment.newInstance(list);
                break;
            case FEEDBACK:
                fragment = FeedbackFragment.newInstance();
                break;
            case ADD_VIDEO:
                fragment = AddVideoFragment.newInstance();
                break;
            case SETTING:
                LoginData loginDataWithSetting = getIntent().getParcelableExtra(DATA);
                fragment = SettingFragment.newInstance(loginDataWithSetting);
                break;
            case HELP_LIST:
                fragment = HelpListFragment.newInstance();
                break;
            case ACCOUNT_MANAGE://账号管理
                fragment = AccountManageFragment.newInstance();
                break;
            case ACCOUNT_CHANGE_PASSWORD://更改密码
                fragment = ChangePasswordFragment.newInstance();
                break;
            case BIND_PHONE://绑定手机号
                fragment = BindPhoneFragment.newInstance();
                break;
            case MY_WORKS:
                fragment = WorksFragment.newInstance();
                break;
            case MY_WORKS_DETAIL:
                WorksListBean workBean = getIntent().getParcelableExtra(DATA);
                fragment = WorksFinishDetailFragment.newInstance(workBean);
                break;
            case MATERIAL_REPERTORY:
                fragment = MaterialManagerFragment.newInstance();
                break;
            case ALTERATION_PHONE://更换手机号
                fragment = AlterationPhoneFragment.newInstance();
                break;
            case FORGOT_PASSWORD://忘记密码
                fragment = ForgotPasswordFragment.newInstance();
                break;
            case COUPON://优惠券
                fragment = CouponFragment.newInstance();
                break;
        }
        if (fragment != null) {
            addFragment(R.id.General_content, fragment, false);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.general_page, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void initActionBar() {
        mToolbar.setNavigationIcon(R.drawable.ic_back);
        super.initActionBar();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        @FragmentType int type = getIntent().getIntExtra(TYPE, HELP);
        menu.findItem(R.id.menu_save).setVisible(type == TEXT_EDIT || type == ORDER_ADD_ADDRESS);
        menu.findItem(R.id.menu_delete).setVisible(type == SHOPPING_CART);
        menu.findItem(R.id.menu_send).setVisible(type == FEEDBACK);
        menu.findItem(R.id.menu_help).setVisible(type == ADD_VIDEO);
        menu.findItem(R.id.menu_skip).setVisible(type == ADD_VIDEO);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        int type = getIntent().getIntExtra(TYPE, HELP);
        if (data == null) {
            super.onActivityResult(resultCode, resultCode, data);
            return;
        }
        Parcelable resultData = data.getParcelableExtra(CONTENT);
        switch (type) {
            case ORDER:
                if (resultData instanceof Invoice) {
                    ((OrderFragment) mCurrentFragment).setInvoice((Invoice) resultData);
                } else if (resultData instanceof AddressData) {
                    ((OrderFragment) mCurrentFragment).setDefaultAddress((AddressData) resultData);
                }
                break;
            case ORDER_ADDRESS:
                ((OrderAddressFragment) mCurrentFragment).setItem((AddressData) resultData);
                break;
            case ORDER_FROM_SHOPPING_CART:
                if (resultData instanceof Invoice) {
                    ((OrderFromShoppingCartFragment) mCurrentFragment).setInvoice((Invoice) resultData);
                } else if (resultData instanceof AddressData) {
                    ((OrderFromShoppingCartFragment) mCurrentFragment).setDefaultAddress((AddressData) resultData);
                }
                break;
            case ADD_VIDEO:
                String videoText = getIntent().getStringExtra(CONTENT);
                ((AddVideoFragment) mCurrentFragment).setText(videoText);
                break;
            case SETTING:
                mCurrentFragment.onActivityResult(requestCode, resultCode, data);
                break;
            case MATERIAL_REPERTORY:
                final ArrayList<BaseMedia> medias = Boxing.getResult(data);
                BaseMedia baseMedia = medias.get(0);
                if (!(baseMedia instanceof ImageMedia)) {
                    return;
                }
                final ImageMedia imageMedia = (ImageMedia) baseMedia;
                ((MaterialManagerFragment) mCurrentFragment).uploadImage(imageMedia.getPath());
                break;
            case BIND_PHONE:
                String phoneNumb = data.getStringExtra(CHANGE_PHONE);
                ((BindPhoneFragment) mCurrentFragment).setTelNum(phoneNumb);
                break;
        }

        switch (requestCode) {
            case UserInfoFragment.REQUEST_ALBUM:
            case UserInfoFragment.REQUEST_CROP:
                if (mCurrentFragment instanceof UserInfoFragment) {
                    mCurrentFragment.onActivityResult(resultCode, resultCode, data);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        int type = getIntent().getIntExtra(TYPE, HELP);
        switch (type) {
            case ORDER:
            case ORDER_FROM_SHOPPING_CART:
            case ADD_VIDEO:
                String message = "", negativeText = "", positiveText = "";
                switch (type) {
                    case ADD_VIDEO:
                        message = "确认返回，放弃此次编辑";
                        negativeText = "我在考虑";
                        positiveText = "忍痛放弃";
                        break;
                    case ORDER:
                    case ORDER_FROM_SHOPPING_CART:
                        message = "确认放弃订单";
                        negativeText = "否";
                        positiveText = "是";
                        break;
                }
                NormalDialogFragment dialog = NormalDialogFragment.newInstance(message, negativeText, positiveText);
                dialog.setOnNegativeClickListener(mOnNegativeListener);
                dialog.show(getSupportFragmentManager(), dialog.getOurTag());
                break;
            case SHOPPING_CART:
            case ORDER_DETAIL:
            case ORDER_SUCCESS:
                toBackActivity();
                break;
            default:
                super.onBackPressed();
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mCurrentFragment != null) {
            int type = getIntent().getIntExtra(TYPE, HELP);
            switch (type) {
                case SETTING:
                    ((MainActivityCallback) mCurrentFragment).refreshData();
                    break;
                case MY_ORDERS:
                    ((MyOrdersFragment) mCurrentFragment).refreshFragment();
                    break;
                case ACCOUNT_MANAGE:
                    ((AccountManageFragment) mCurrentFragment).update();
                    break;
            }
        }
    }

    private Action0 mOnNegativeListener = new Action0() {
        @Override
        public void call() {
            toBackActivity();
        }
    };

}
